package service

import (
	"context"
	"log"
	"user-service/basic/config"
	"user-service/handler/model"

	__ "user-service/basic/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) GetUsernameByUsername(_ context.Context, in *__.GetUsernameByUsernameReq) (*__.GetUsernameByUsernameResp, error) {
	var user model.User
	if err := user.GetUsernameBUsername(config.DB, in.Username); err != nil {
		return nil, err
	}
	return &__.GetUsernameByUsernameResp{
		Username: user.Username,
		Password: user.Password,
		UserId:   int64(user.ID),
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	user := model.User{
		Username: in.Username,
		Password: in.Password,
	}
	if err := user.CreateUser(config.DB); err != nil {
		return nil, err
	}
	return &__.CreateUserResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *Server) CouponCreate(_ context.Context, in *__.CouponCreateReq) (*__.CouponCreateResp, error) {
	if in.Title != "" {
		log.Println("标题不能为空")
	}
	if in.Quantity != 0 {
		log.Println("发送总量不能为空")
	}
	coupon := model.Coupon{
		Title:       in.Title,
		TypeId:      in.TypeId,
		Comment:     in.Comment,
		ClaimStatus: in.ClaimStatus,
		Remaining:   in.Remaining,
		Quantity:    in.Quantity,
	}
	if err := coupon.CreateCoupon(config.DB); err != nil {
		return nil, err
	}
	return &__.CouponCreateResp{
		CouponId: int64(coupon.ID),
	}, nil
}
func (s *Server) CouponList(_ context.Context, in *__.CouponListReq) (*__.CouponListResp, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	var coupon []model.Coupon
	query := config.DB.Offset(int(offset)).Limit(int(pageSize)).Order("created_at desc")
	if in.Title != "" {
		query.Where("title like ?", in.Title)
	}
	if in.TypeId != 0 {
		query.Where("type_id=?", in.TypeId)
	}
	query.Find(&coupon)
	var couponlists []*__.CouponList
	for _, c := range coupon {
		list := __.CouponList{
			Title:       c.Title,
			TypeId:      c.TypeId,
			Comment:     c.Comment,
			ClaimStatus: c.ClaimStatus,
			Remaining:   c.Remaining,
			Quantity:    c.Quantity,
		}
		couponlists = append(couponlists, &list)
	}
	return &__.CouponListResp{
		List: couponlists,
	}, nil
}
func (s *Server) CouponUpdate(_ context.Context, in *__.CouponUpdateReq) (*__.CouponUpdateResp, error) {
	var coupon model.Coupon
	if err := coupon.UpdateCoupon(config.DB, in.Id, in.Status); err != nil {
		return nil, err
	}
	return &__.CouponUpdateResp{
		Id: in.Id,
	}, nil
}
func (s *Server) CouponDel(_ context.Context, in *__.CouponDelReq) (*__.CouponDelResp, error) {
	var coupon model.Coupon
	err := config.DB.Where("id=?", in.Id).Delete(&coupon).Error
	if err != nil {
		return nil, err
	}
	return &__.CouponDelResp{}, nil
}
